<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发布论坛</title>
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body class="layui-layout-body">
<div style="margin-top: 50px;>
<!-- 示例-970 -->
<ins class="adsbygoogle" style="display:inline-block;width:970px;height:90px" data-ad-client="ca-pub-6111334333458862" data-ad-slot="3820120620"></ins>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
    <legend>资讯信息</legend>
</fieldset>

<form class="layui-form" action="" lay-filter="example" >


     <div class="layui-form-item">
        <label class="layui-form-label">标题<span id="id" style="visibility: hidden"></span></label>
        <div class="layui-input-block">
            <input type="text" id="title" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <input type="text" id="content" name="content" lay-verify="content" autocomplete="off" placeholder="请输入内容" class="layui-input">
        </div>
    </div>




    <div class="layui-form-item">
        <div class="layui-input-block">
<!--            <button type="button" class="layui-btn layui-btn-normal" id="LAY-component-form-setval">赋值</button>-->
            <button type="button" class="layui-btn layui-btn-normal" id="LAY-component-form-getval">提交</button>
<!--            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>-->
        </div>
    </div>
</form>

<script src="layui/layui.all.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate',"upload",], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,upload = layui.upload
            ,laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            account: function(value){
                if(value.length < 5){
                    return '账号至少得5个字符啊';
                }
            }
            ,pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function(data){
            layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        // //监听提交
        // form.on('submit(demo1)', function(data){
        //     $.ajax({
        //         url:"http://localhost:8080/user/register",
        //         data:{'account':"sadasdasddsas","password":"sdasdas"},
        //         type:"Post",
        //
        //         success:function(data){
        //             console.log(data);
        //             layer.msg(data.msg);
        //             location.reload(); //删除成功后再刷新
        //         },
        //         error:function(data){
        //             $.messager.alert('错误',data.msg);
        //         }
        //     });
        //     // layer.alert(JSON.stringify(data.field), {
        //     //     title: '最终的提交信息'
        //     // })
        //     return false;
        // });
        var head_pic="";
        var uploadInst = upload.render({
            elem: '#headImg'
            , url: 'http://http://192.168.1.6:8080/upload'
            , size: 500
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    layui.$('#demo1').attr('src', result); //图片链接（base64）
                });

            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                layer.msg('上传成功');
                head_pic="http://192.168.1.6:8080/"+res.data;
                //上传成功
                //打印后台传回的地址: 把地址放入一个隐藏的input中, 和表单一起提交到后台, 此处略..
                /*   console.log(res.data.src);*/
                // window.parent.uploadHeadImage(res.data);
                // var demoText = $('#demoText');
                // demoText.html('<span style="color: #8f8f8f;">上传成功!!!</span>');
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        var localTest = layui.data('user');
        console.log(localTest.id); //获得“ABC”
        var company=null;
        layui.jquery.ajax({
            url:"http://localhost:8080/company/getCompanyInfo",
            data:{"id":localTest.id},
            type:"Post",
            success:function(result){
                company= result.data;

            },
            error:function(result){
                // $.messager.alert('错误',result.msg);
            }
        });


        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function(){
            var data = form.val('example');

            layui.jquery.ajax({
                url:"http://localhost:8080/luntan/addLuntan",
                data:{'title':data.title,"content":data.content,"uid":company.id,"uname":company.name,"uimg":company.img},
                type:"Post",
                success:function(result){

                    console.log(result);
                    layer.msg(result.msg);
                    // location.reload(); //删除成功后再刷新
                },
                error:function(result){
                    // $.messager.alert('错误',result.msg);
                }
            });



        });

    });
</script>

</body>
</html>